Learn R Programming

wrapr (version 1.9.6)

[<-.Unpacker: Unpack or bind values into the calling environment.

Description

Unpacks or binds values into the calling environment. Uses bquote escaping. NULL is a special case that is unpacked to all targets. NA targets are skipped. All non-NA target names must be unique.

Usage

# S3 method for Unpacker
[(wrapr_private_self, ...) <- value

Arguments

wrapr_private_self

object implementing the feature, wrapr::unpack

...

names of to unpack to (can be escaped with bquote .() notation).

value

list to unpack into values, must have a number of entries equal to number of ... arguments

Value

wrapr_private_self

Details

Note: when using []<- notation, a reference to the unpacker object is written into the unpacking environment as a side-effect of the implied array assignment. Array-assign form can not use the names: ., wrapr_private_self, value, or the name of the unpacker itself. For more details please see here http://www.win-vector.com/blog/2020/01/unpack-your-values-in-r/.

Related work includes Python tuple unpacking, zeallot's arrow, and vadr::bind.

Examples

Run this code
# NOT RUN {
# named unpacking
# looks like assignment: DESTINATION = NAME_VALUE_USING
d <- data.frame(x = 1:2,
                g=c('test', 'train'),
                stringsAsFactors = FALSE)
to[train_set = train, test_set = test] <- split(d, d$g)
# train_set and test_set now correctly split
print(train_set)
print(test_set)
rm(list = c('train_set', 'test_set'))

# named unpacking NEWNAME = OLDNAME implicit form
# values are matched by name, not index
to[train, test] <- split(d, d$g)
print(train)
print(test)
rm(list = c('train', 'test'))


# }

Run the code above in your browser using DataLab